﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace Phone
{
    //地址信息编辑界面
    public partial class AddrInfo : Form
    {
        private int n = -1;

        //默认构造函数
        public AddrInfo()
        {
            InitializeComponent();
        }

        //带参数构造函数,参数为地址编号
        public AddrInfo(int n)
        {
            InitializeComponent();
            this.n = n;
        }

        //窗体加载时读取地址信息或当前位置信息并显示
        private void AddrInfo_Load(object sender, EventArgs e)
        {
            if (n == -1)
            {
                textBox1.Text = Vars.gps.jd.ToString();
                textBox2.Text = Vars.gps.wd.ToString();
                textBox4.Text = Vars.gps.hb.ToString();
            }
            else
            {
                textBox1.Text = Vars.address[n].gps.jd.ToString();
                textBox2.Text = Vars.address[n].gps.wd.ToString();
                textBox4.Text = Vars.address[n].gps.hb.ToString();
                textBox3.Text = Vars.address[n].address;
            }
        }

        //窗体激活时选中地址信息输入框
        private void AddrInfo_Activated(object sender, EventArgs e)
        {
            textBox3.Focus();
        }

        //窗体关闭时退出程序
        private void AddrInfo_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (Form.ActiveForm == this) Application.Exit();
        }

        //返回地址簿界面
        private void button1_Click(object sender, EventArgs e)
        {
            Addresses addresses = new Addresses();
            addresses.Show();
            addresses.Location = this.Location;
            this.Close();
        }

        //更新当前位置信息
        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Text = Vars.gps.jd.ToString();
            textBox2.Text = Vars.gps.wd.ToString();
            textBox4.Text = Vars.gps.hb.ToString();
        }

        //保存地址信息
        private void button3_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == ""|| textBox4.Text == "")
            {
                MessageBox.Show("请填写完整信息");
                return;
            }
            //检查输入合法性
            double d;
            if (!double.TryParse(textBox1.Text, out d) || !double.TryParse(textBox2.Text, out d) || double.Parse(textBox1.Text) < -180 || double.Parse(textBox1.Text) > 180 || double.Parse(textBox2.Text) < -90 || double.Parse(textBox2.Text) > 90|| double.Parse(textBox4.Text) < 0 || double.Parse(textBox4.Text) > 1000000000)
            {
                MessageBox.Show("GPS定位输入不合法");
                return;
            }
            for (int i = 0; i < Vars.address_count; i++)
            {
                if (i == n) continue;
                if ((Vars.address[i].gps.jd == double.Parse(textBox1.Text) && Vars.address[i].gps.wd == double.Parse(textBox2.Text) && Vars.address[i].gps.hb == double.Parse(textBox4.Text))|| Vars.address[i].address == textBox3.Text)
                {
                    MessageBox.Show("地址信息已存在,保存失败");
                    return;
                }
            }
            if (n == -1)
            {
                if (Vars.address_count >= Defines.MAX_ADDRESS_COUNT)
                {
                    MessageBox.Show("地址信息已满,保存失败");
                    return;
                }
                n = Vars.address_count;
                Vars.address_count++;
            }
            Vars.address[n].gps.jd = double.Parse(textBox1.Text);
            Vars.address[n].gps.wd = double.Parse(textBox2.Text);
            Vars.address[n].gps.hb = double.Parse(textBox4.Text);
            Vars.address[n].address = textBox3.Text;
            MessageBox.Show("保存成功");
            button1_Click(sender, e);
        }
    }
}
